import { inject, injectable } from "inversify";
import { PrismaDB } from "../db";
import { PostDto } from "./post.dto";
import { plainToClass } from "class-transformer"; //把值合并到类中
import { validate } from "class-validator"; //验证
//提供一个注射器，用于创建UserService的实例
@injectable()
export class PostService {
  constructor(@inject(PrismaDB) private PrismaDB: PrismaDB) {}
  public async getList() {
    return this.PrismaDB.prisma.post.findMany();
  }
  public async createPost(post: PostDto) {
    let postDto = plainToClass(PostDto, post);
    const errors = await validate(postDto);
    if (errors.length) {
      return errors;
    } else {
      return await this.PrismaDB.prisma.post.create({
        data: post,
      });
    }
  }
}
